package org.kalai.unifypay.tfb.pay;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;

import org.kalai.unifypay.util.CipherUtils;

public class Md5Util {

   public static void main(String[] args) {
      String s = "item_name=zsl-测试&listid=1021800776625161020000023304&notify_type=1&pay_type=800201&sp_billno=OR1020173906065168437&sysd_time=20161020173929&tran_amt=10&tran_state=1&tran_time=20161020053906&key=12345";
      System.out.println(Md5Util.getMD5(s, "GBK"));
      System.err.println(s.getBytes());
   }

   /**
    * MD5加密
    */
   public static String getMD5(String str) {
      return getMD5(str, "UTF-8");
   }

   /**
    * MD5加密
    */
   public static String getMD5(String str, String encode) {
      if (null == str)
         return null;
      MessageDigest messageDigest = null;
      try {
         messageDigest = MessageDigest.getInstance("MD5");
         messageDigest.reset();
         messageDigest.update(str.getBytes(encode));
      } catch (Exception e) {
         e.printStackTrace();
         return null;
      }
      byte[] byteArray = messageDigest.digest();
      StringBuffer md5StrBuff = new StringBuffer();
      for (int i = 0; i < byteArray.length; i++) {
         if (Integer.toHexString(0xFF & byteArray[i]).length() == 1)
            md5StrBuff.append("0").append(Integer.toHexString(0xFF & byteArray[i]));
         else
            md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));
      }
      return md5StrBuff.toString();
   }

}
